package com.zsl.dao.impl;

import com.zsl.dao.OrderItemDao;
import com.zsl.model.OrderInfo;
import com.zsl.model.OrderItem;
import com.zsl.util.C3P0Util;
import com.zsl.util.DbHelper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderItemDaoImpl implements OrderItemDao {
    //赵磊
    @Override
    public String getOrderItemCount(String[] whereColumn, String[] whereValue) throws SQLException {
        String count = null;
        StringBuilder sql = null;
        sql = new StringBuilder("select count(*) from orderitem,orderinfo where orderitem.orderInfo_id = orderinfo.id");
        if (whereColumn != null) {
            for (String wc : whereColumn) {
                sql.append(" AND ").append(wc).append(" =?");
            }
        }
        ResultSet rs = DbHelper.executeQuery(sql.toString(), whereValue);
        while (rs.next()) {
            count = rs.getString(1);
        }
        //关闭连接
        C3P0Util.release(DbHelper.getPs(), DbHelper.getConn(), rs);
        return count;
    }

    @Override
    public List<OrderItem> getOrderItem(String[] whereColumn, String[] whereValue) throws SQLException {
        StringBuilder sql = null;
        List<OrderItem> orderItems = new ArrayList<>();

        sql = new StringBuilder("select * from orderitem,book where orderitem.book_id=book.id");
        if (whereColumn != null) {
            for (String wc : whereColumn) {
                sql.append(" AND ").append(wc).append(" =?");
            }
        }
        ResultSet rs = DbHelper.executeQuery(sql.toString(), whereValue);
        while (rs.next()) {
            OrderItem orderItem = new OrderItem();
            orderItem.setId(rs.getString("orderitem.id"));
            orderItem.setNum(Integer.parseInt(rs.getString("num")));
            orderItem.setPrice(rs.getFloat("orderitem.price"));
            orderItem.setOrderInfo_id(rs.getString("orderInfo_id"));
            orderItem.setBook_id(rs.getString("book_id"));
            orderItem.setBook_generate_name(rs.getString("name"));
            orderItems.add(orderItem);
        }
        //关闭连接
        C3P0Util.release(DbHelper.getPs(), DbHelper.getConn(), rs);
        return orderItems;
    }

    @Override
    public Map<String, Object> insertOrderItem(List<OrderItem> orderItems) {
        String sql = "INSERT INTO orderitem (id,num,price,orderInfo_id,book_id,is_deleted) VALUES(?,?,?,?,?,'0')";
        List<Integer> line = new ArrayList<>();
        List<Float> price = new ArrayList<>();
        List<String> orderItem_id = new ArrayList<>();
        for (OrderItem orderItem : orderItems) {
            line.add(DbHelper.executeUpdate(sql, new Object[]{orderItem.getId(), orderItem.getNum(), orderItem.getPrice(), orderItem.getOrderInfo_id(), orderItem.getBook_id()}));
            orderItem_id.add(orderItem.getId());
            price.add(orderItem.getPrice());
        }
        Map<String, Object> result = new HashMap<>();
        result.put("line", line);
        result.put("orderItem_id", orderItem_id);
        result.put("price", price);
        return result;
    }

    @Override
    public Map<String, Object> updateOrderItemById(List<OrderItem> orderItems, int selectModel) {
        List<String> orderitem_id = new ArrayList<>();
        Map<String, Object> result = new HashMap<>();
        List<Integer> line = new ArrayList<>();
        //update num,price
        if (selectModel == 1) {
            for (OrderItem orderItem : orderItems) {
                orderitem_id.add(orderItem.getId());
                line.add(DbHelper.update("orderitem", new String[]{"num", "price"},
                        new String[]{orderItem.getNum().toString(), orderItem.getPrice().toString()},
                        new String[]{"id"},
                        new String[]{orderItem.getId()}));
            }
            result.put("line", line);
            result.put("orderitem_id", orderitem_id);
        }
        return result;
    }

    @Override
    public Integer deleteOrderItemById(String item_id) {
        String sql = "delete from orderitem where id = ?";
        return DbHelper.executeDelete(sql, new Object[]{item_id});
    }
}
